/**
 * @公司:公约树
 * @位置:苏州
 * @Classname Anime01
 * @Date 2022-06-09 下午 11:56
 * @Created by hanWanLu
 * @auther 中文:韩万路
 */


package com.animeJavaExample;

import com.animeJava.core.AnimeJava;
import com.animeJava.core.EasingData;
import com.animeJava.enumData.DirectionEnum;
import com.animeJava.mode.animeModel.AnimeProperty;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;


/**
 * 基础动画示例
 */
public class Anime01  extends Application {
    public static void main(String[] args) {

        launch(args);

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setHeight(500);
        primaryStage.setWidth(500);

        AnimeProperty animeProperty = new AnimeProperty();
        //动画开始执行
        animeProperty.setBegin((msg)->{
            System.out.println("动画开始----");
        });
        Button button = createButton(120);
        button.setLayoutY(0);
        button.setLayoutX(100);
        Scale scale = new Scale();
        button.getTransforms().add(scale);
        anchorPane.getChildren().add(button);
        //动画更新中执行
        animeProperty.setUpdate((timeTo,animeInfo, data)->{
            Platform.runLater(()->{
                try{
                    if(data.get(0) != null)
                        button.setLayoutY(10+data.get(0));

                    if(data.get(1) != null)
                        scale.setX(data.get(1));
                }catch (Exception e){
                    e.printStackTrace();
                }
                if(data.get(2) != null)
                    scale.setY(data.get(2));

                scale.setPivotX(60);
                scale.setPivotY(110);
            });
            return (button.getScene() == null)?true:false;
        });
        //动画结束执行
        animeProperty.setComplete((msg)->{
            System.out.println("ok");
        });

        animeProperty.addKeyFrames(0,0D , null , null);
        animeProperty.addKeyFrames(95,null,1D ,1D);
        animeProperty.addKeyFrames(100,200D ,1.2 , 0.9);

        animeProperty.setDelay(400);            //延时执行 ok
        animeProperty.setDuration(700);        //总共耗时 ok
        animeProperty.setAutoplay(true);        //自动播放 ok
        animeProperty.setLoop(true);            //循环播放 ok
        animeProperty.setDirection(DirectionEnum.ALTERNATE);        //往返    ok

        animeProperty.setEasing(new EasingData(0.61, 0.09, 0.02, 0.99));
        AnimeJava animeJava = new AnimeJava(animeProperty);


        Button button1 = createButton(80);
        anchorPane.getChildren().add(button1);
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                animeJava.play();
            }
        });
        Slider slider = new Slider();
        anchorPane.getChildren().add(slider);
        slider.setMax(1);
        slider.setMin(0);
        slider.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("24----"+slider.getValue());
                animeJava.setKeyRange(slider.getValue());
            }
        });


        primaryStage.show();
    }

    private Label createNode(String color , Double x , Double y){
        Label label = new Label();
        label.setStyle("-fx-background-color:"+color+"; -fx-background-radius: 10px");
        int size = 10;
        label.setPrefHeight(size);
        label.setPrefWidth(size);
        label.setMaxHeight(size);
        label.setMaxWidth(size);
        label.setMinHeight(size);
        label.setMinWidth(size);

        label.setLayoutY(100+x);
        label.setLayoutX(100+y);
        return label;
    }

    private Button createButton(int size){
        Button button = new Button();
        button.setPrefWidth(size);
        button.setPrefHeight(size);

        button.setMinWidth(size);
        button.setMinHeight(size);

        button.setMaxWidth(size);
        button.setMaxHeight(size);

        button.setStyle("-fx-background-radius: 200px ; -fx-border-radius: 200px");

        return button;
    }
}
